package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.CollectiveSales;
import com.sc.nft.enums.ConSignStatus;
import com.sc.nft.mapper.CollectiveSalesMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 集售挂单表(sc_collective_sales)数据DAO
 *
 * @author zxy
 * @since 2024-09-17 20:51:05
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class CollectiveSalesDao extends ServiceImpl<CollectiveSalesMapper, CollectiveSales> {

    public Integer getCollectionOnConsignCount(Long collectionId){
        return lambdaQuery().eq(CollectiveSales::getCollectiveDate, DateTime.now().toString("yyyy-MM-dd")).eq(CollectiveSales::getCollectionId,collectionId).eq(CollectiveSales::getStatus, ConSignStatus.ON_SALES).count();
    }

    public Integer getCollectionSumCount(Long collectionId){
        return lambdaQuery().eq(CollectiveSales::getCollectiveDate, DateTime.now().toString("yyyy-MM-dd")).eq(CollectiveSales::getCollectionId,collectionId).in(CollectiveSales::getStatus, ConSignStatus.ON_SALES,ConSignStatus.LOCK,ConSignStatus.SUCCESS).count();
    }
    public Integer getUserCollectionOnConsignCount(Long userId,Long collectionId){
        return lambdaQuery().eq(CollectiveSales::getCollectiveDate, DateTime.now().toString("yyyy-MM-dd")).eq(CollectiveSales::getUserId,userId).eq(CollectiveSales::getCollectionId,collectionId).eq(CollectiveSales::getStatus, ConSignStatus.ON_SALES).count();
    }

    public Integer getUserCollectionCanRevokeCount(Long userId,Long collectionId){
        return lambdaQuery().eq(CollectiveSales::getCollectiveDate, DateTime.now().toString("yyyy-MM-dd")).eq(CollectiveSales::getUserId,userId).eq(CollectiveSales::getCollectionId,collectionId).eq(CollectiveSales::getStatus, ConSignStatus.ON_SALES).count();
    }


    public List<CollectiveSales> getUserCollectionOnConsignList(Long userId,Long collectionId){
        return lambdaQuery().eq(CollectiveSales::getCollectiveDate, DateTime.now().toString("yyyy-MM-dd")).eq(CollectiveSales::getUserId,userId).eq(CollectiveSales::getCollectionId,collectionId).eq(CollectiveSales::getStatus, ConSignStatus.ON_SALES).list();
    }


    public List<CollectiveSales> getUserCollectionOnConsignListLimit(Long userId,Long collectionId,Integer count){
        return lambdaQuery().eq(CollectiveSales::getCollectiveDate, DateTime.now().toString("yyyy-MM-dd")).eq(CollectiveSales::getUserId,userId).eq(CollectiveSales::getCollectionId,collectionId).eq(CollectiveSales::getStatus, ConSignStatus.ON_SALES).last(" limit "+count).list();
    }

    public CollectiveSales getByUserCollectionId(Long userId,Long userCollectionId){
        return lambdaQuery().eq(CollectiveSales::getCollectiveDate, DateTime.now().toString("yyyy-MM-dd")).eq(CollectiveSales::getUserId,userId).eq(CollectiveSales::getUserCollectionId,userCollectionId).eq(CollectiveSales::getStatus, ConSignStatus.ON_SALES).one();
    }

    public List<CollectiveSales> getOnConsignListLimitNotEqUserId(Long collectionId,Integer count,Long userId){
        return lambdaQuery().eq(CollectiveSales::getCollectionId,collectionId).eq(CollectiveSales::getStatus, ConSignStatus.ON_SALES).ne(CollectiveSales::getUserId,userId).last(" limit "+count).list();
    }
    public List<CollectiveSales> getAutoCloseListLimit(Integer count){
        return lambdaQuery().lt(CollectiveSales::getOffSalesTime,DateTime.now()).eq(CollectiveSales::getStatus, ConSignStatus.ON_SALES).last(" limit "+count).list();
    }

    public Integer getCollectionDealCount(Long collectionId){
        return lambdaQuery().eq(CollectiveSales::getCollectiveDate, DateTime.now().toString("yyyy-MM-dd")).eq(CollectiveSales::getCollectionId,collectionId).eq(CollectiveSales::getStatus, ConSignStatus.SUCCESS).count();
    }
    public Integer getUserCollectionDealCount(Long userId,Long collectionId){
        return lambdaQuery().eq(CollectiveSales::getCollectiveDate, DateTime.now().toString("yyyy-MM-dd")).eq(CollectiveSales::getUserId,userId).eq(CollectiveSales::getCollectionId,collectionId).eq(CollectiveSales::getStatus, ConSignStatus.SUCCESS).count();
    }
    public Integer getUserCollectionSumCount(Long userId,Long collectionId){
        return lambdaQuery().eq(CollectiveSales::getCollectiveDate, DateTime.now().toString("yyyy-MM-dd")).eq(CollectiveSales::getUserId,userId).eq(CollectiveSales::getCollectionId,collectionId).in(CollectiveSales::getStatus,ConSignStatus.ON_SALES,ConSignStatus.LOCK,ConSignStatus.SUCCESS).count();
    }
    public Boolean updateStatusToCancel(Long id){
        return lambdaUpdate().eq(CollectiveSales::getId,id).set(CollectiveSales::getStatus,ConSignStatus.CANCEL).eq(CollectiveSales::getStatus,ConSignStatus.ON_SALES).update();
    }

    public Boolean updateStatusToLock(Long id){
        return lambdaUpdate().eq(CollectiveSales::getId,id).set(CollectiveSales::getStatus,ConSignStatus.LOCK).eq(CollectiveSales::getStatus,ConSignStatus.ON_SALES).update();
    }

    public Boolean updateStatusToConsign(Long id){
        return lambdaUpdate().eq(CollectiveSales::getId,id).set(CollectiveSales::getStatus,ConSignStatus.ON_SALES).eq(CollectiveSales::getStatus,ConSignStatus.LOCK).update();
    }


}